<template>
  <iot-card
    class="today-blk device-blk mb-20"
    title="报警视频"
    :moreType="1"
    @more="getMore"
  >
    <div class="monitor-blk" ref="monitorBlk">
      <div class="video-list flex wrap" v-if="videoData.number">
        <div class="flex-1">
          <livePlayer :options="videoData" :initRender="true"></livePlayer>
        </div>
      </div>
      <p class="data-none" v-else-if="!loading">暂无监控视频</p>
    </div>
    <el-dialog
      title="视频"
      :visible="videoVisible"
      :width="1000 * scale + 'px'"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      destroy-on-close
      append-to-body
      @close="closeAction"
    >
      <video-item :monitorList="monitorList"></video-item>
    </el-dialog>
  </iot-card>
</template>

<script>
import iotCard from "@/components/iot/card";
import { mapState } from "vuex";
import livePlayer from "@/components/screenVideoPlayer/player";
import VideoItem from "./videoItem.vue";
export default {
  components: {
    iotCard,
    livePlayer,
    VideoItem,
  },
  props: {
    monitorList: {
      type: Array,
      default() {
        return [];
      },
    },
  },
  computed: {
    ...mapState(["scale", "winSize"]),
  },
  data() {
    return {
      loading: false,
      productId: "",
      videoData: {},
      videoVisible: false,
    };
  },
  mounted() {
    if (this.monitorList[0].playType) {
      this.productId = this.monitorList[0].productId;
      this.videoData = this.formatVideoOptions(
        this.monitorList[0].src,
        this.monitorList[0].playType,
        this.monitorList[0].number
      );
      console.log(this.videoData);
      return;
    }
    this.getCameraDetail();
  },
  methods: {
    closeAction() {
      this.videoVisible = false;
    },
    getMore() {
      console.log(111);
      this.videoVisible = true;
    },
    formatVideoOptions(url, playType, number) {
      const typeMap = this.config.videoTypeMap;
      const options = {
        playType,
        sources: [
          {
            src: url,
            type: typeMap[playType],
          },
        ],
        number,
        productId: this.productId || "",
      };
      return options;
    },
    getCameraDetail() {
      let number = this.monitorList[0].cameraNumber;
      this.loading = true;
      this.$get("/fireFightingIOT/device/detail", {
        deviceClassify: "3",
        number,
        id: "",
      }).then(
        (res) => {
          console.log(res);
          this.loading = false;
          let deviceDetail = res.data;
          this.productId = deviceDetail.productId;
          if (deviceDetail.videoplayAddr) {
            this.videoData = this.formatVideoOptions(
              deviceDetail.videoplayAddr,
              deviceDetail.playType,
              number
            );
          } else if (deviceDetail.videoplayAddrList.length > 0) {
            this.videoData = {
              ...deviceDetail.videoplayAddrList[0],
              playType: deviceDetail.playType,
              number,
            };
          } else {
            this.getVideoStream(number, deviceDetail.playType);
          }
          console.log(this.videoData);
        },
        (err) => {
          this.loading = true;
          if (err.msg) {
            this.$message.error(err.msg);
          }
        }
      );
    },
    getVideoStream(number, playType) {
      this.$get("/fireFightingIOT/device/getVideoStream", { number })
        .then((res) => {
          if (res.data) {
            this.videoData = this.formatVideoOptions(
              res.data,
              playType,
              number
            );
            console.log(this.videoData);
          }
        })
        .catch((err) => {
          this.$message.error(err.msg || "系统错误");
        });
    },
  },
};
</script>

<style lang="less" scoped>
/deep/.context {
  height: 94%;
  display: flex;
  align-items: center;
  justify-content: center;
}
/deep/.video-list {
  width: 100%;
  height: 100%;
}
/deep/.monitor-blk {
  width: 100%;
  height: 90%;
}
</style>
